package com.eventos.error;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;



public class MailUtils {
    private static final byte[] KEY = {115, 36, 69, 104, 53, 34, 105};

    
    public static void sendMail(final String to,final String subject, final File... files) throws Exception {
    	final byte[][] bytes = new byte[files.length][];
    	final String[] filenames = new String[files.length];
    	int i = 0;
		for (final File file : files) {
    		filenames[i]=file.getName();
    		bytes[i++]=IOUtils.getBytes(file);
		}
		sendMail( to, subject,null,  bytes,  filenames);
    }
    
	



	public static void sendMail(final String to,final String subject, final String body, final byte[][] files, final String[] filenames) throws Exception {
		// create some properties and get the default Session
		final Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		final Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("eventos.oron3","r981306o");
					}
				});
		// create a message
		final MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("Eventos-Support@do_not_replay.com"));
		final InternetAddress[] address = {new InternetAddress(to)};
		msg.setRecipients(Message.RecipientType.TO, address);
		msg.setSubject(subject);
		msg.reply(false);
		
		Multipart mp = null;
		if (files!=null) {
			mp = new MimeMultipart();
			for (int  i = 0 ; i < files.length ; i++) {
				final byte[] filedata = files[i];
				if (filedata != null)
				{
					final MimeBodyPart mbp = new MimeBodyPart();
					final ByteArrayDataSource fds = new ByteArrayDataSource(filedata, "application/octet-stream");
					mbp.setDataHandler(new DataHandler(fds));
					mbp.setFileName(filenames[i]);
					mp.addBodyPart(mbp);
				}
			}
			
			// add the Multipart to the message
	        msg.setContent(mp);
			
		}
		if (body!=null ) 
			if (mp==null) msg.setText(body);
			else {
				final BodyPart messageBodyPart = new MimeBodyPart();
		        messageBodyPart.setText(body);
		        messageBodyPart.setContent(body, "text/html");
		        mp.addBodyPart(messageBodyPart);
			}
		

		// set the Date: header
		msg.setSentDate(new Date());

		/*
		 * If you want to control the Content-Transfer-Encoding
		 * of the attached file, do the following.  Normally you
		 * should never need to do this.
		 *
            msg.saveChanges();
            mbp2.setHeader("Content-Transfer-Encoding", "base64");
		 */

		// Get the password from the key (kindof Hide from source snippers)
		final StringBuilder pass = new StringBuilder();
		for (int i = 0; i < KEY.length; i++) {
			pass.append((char)(KEY[i] ^ 17));
		}
		
		// send the message
		Transport.send(msg);

	}
	public static void sendMail(final String to, final String subject, final String body) throws Exception {
		sendMail( to, subject,body,  null,  null);
	}
	public static void sendMail(final String to, final String subject, final String body,final File... files) throws Exception {
		    	final byte[][] bytes = new byte[files.length][];
		    	final String[] filenames = new String[files.length];
		    	int i = 0;
				for (final File file : files) {
		    		filenames[i]=file.getName();
		    		bytes[i++]=IOUtils.getBytes(file);
				}
				sendMail( to, subject,body,  bytes,  filenames);
	}


}
